package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.act.*;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class RankActBaseData {
    private static Logger logger = LoggerFactory.getLogger(RankActBaseData.class);

    private static ConcurrentHashMap<Integer, ActConfig> actConfigMap = new ConcurrentHashMap<>(); // 限时活动表
    private static ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActConfig>> actConfigGroupMap = new ConcurrentHashMap<>(); // 限时活动表
    public static ConcurrentHashMap<Integer, Integer> actDaysMap = new ConcurrentHashMap<>();

    public static void initBaseData(List<Future> futures) {
        futures.add(initActConfig());
        futures.add(initActTopRanking());
        futures.add(initActRechargeTaskType());
        futures.add(initActRechargeTask());
        futures.add(initActRecharge());
    }

    /**
     * 初始化限时活动表
     */
    public static Future<Void> initActConfig() {
        String table = "act_config";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ActConfig> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActConfig>> groupTmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, Integer> daysTmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ActConfig entity = new ActConfig(result.getJsonObject(i));
                        tmp.put(entity.getConfigId(), entity);
                        groupTmp.computeIfAbsent(entity.getGroupId(), k -> new ConcurrentHashMap<>()).put(entity.getConfigId(), entity);
                        daysTmp.merge(entity.getGroupId(), entity.getEndDay(), Integer::max);
                    }
                    actConfigMap = tmp;
                    actConfigGroupMap = groupTmp;
                    actDaysMap = daysTmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ActConfig getActConfig(int id) {
        ActConfig result = actConfigMap.get(id);
        if (result == null) {
            logger.error("getActConfig error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ActConfig> getActConfigMap() { return actConfigMap; }

    public static ConcurrentHashMap<Integer, ActConfig> getActConfigList(int groupId) {
        if (!actConfigGroupMap.containsKey(groupId)) {
            logger.error("getActConfigList error groupId = " + groupId);
            return null;
        }
        return actConfigGroupMap.get(groupId);
    }


    public static int getActDays(int groupId) {
        if (!actDaysMap.containsKey(groupId)) {
            logger.error("getActDays error groupId = " + groupId);
            return 0;
        }
        return actDaysMap.get(groupId);
    }
    private static ConcurrentHashMap<Integer, ActTopRanking> actTopRankingMap = new ConcurrentHashMap<>(); // 冲榜总表
    private static ConcurrentHashMap<Integer, List<ActTopRanking>> actTopRankingGroupMap = new ConcurrentHashMap<>(); // 冲榜总表

    /**
     * 初始化冲榜总表
     */
    public static Future<Void> initActTopRanking() {
        String table = "act_top_ranking";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ActTopRanking> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<ActTopRanking>> tmpGroup = new ConcurrentHashMap<>(); // 冲榜总表
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ActTopRanking entity = new ActTopRanking(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        tmpGroup.computeIfAbsent(entity.getTopId(), k -> new ArrayList<>()).add(entity);
                    }
                    actTopRankingMap = tmp;
                    actTopRankingGroupMap = tmpGroup;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ActTopRanking getActTopRanking(int id) {
        ActTopRanking result = actTopRankingMap.get(id);
        if (result == null) {
            logger.error("getActTopRanking error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ActTopRanking> getActTopRankingMap() { return actTopRankingMap; }

    public static List<ActTopRanking> getActTopRankingList(int topId) {
        return actTopRankingGroupMap.get(topId);
    }

    public static ActTopRanking getActTopRankingByType(int topId, int type) {
        List<ActTopRanking> rankingList = getActTopRankingList(topId);
        if (rankingList == null || rankingList.isEmpty()) {
            logger.error("getActTopRankingByType error topId = " + topId);
            return null;
        }
        return rankingList.stream().filter(r -> r.getType() == type).findFirst().orElse(null);
    }

    public static ActConfig getActConfigByTopId(int groupId, int topId) {
        ConcurrentHashMap<Integer, ActConfig> actConfigList = getActConfigList(groupId);
        if (actConfigList == null || actConfigList.isEmpty()) {
            return null;
        }
        return actConfigList.get(topId);
    }
    private static ConcurrentHashMap<Integer, ActRecharge> actRechargeMap = new ConcurrentHashMap<>(); // 限时充值配置表
    private static ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActRecharge>> actRechargeGroupMap = new ConcurrentHashMap<>(); // 限时充值配置表

    /**
     * 初始化限时充值配置表
     */
    public static Future<Void> initActRecharge() {
        String table = "act_recharge";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ActRecharge> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActRecharge>> groupTmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ActRecharge entity = new ActRecharge(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        groupTmp.computeIfAbsent(entity.getRechargeId(), k -> new ConcurrentHashMap<>()).put(entity.getType(), entity);
                    }
                    actRechargeMap = tmp;
                    actRechargeGroupMap = groupTmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ActRecharge getActRecharge(int id) {
        ActRecharge result = actRechargeMap.get(id);
        if (result == null) {
            logger.error("getActRecharge error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ActRecharge> getActRechargeMap() { return actRechargeMap; }
    public static ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActRecharge>> getActRechargeGroupMap() { return actRechargeGroupMap; }
    public static ConcurrentHashMap<Integer, ActRecharge> getActRechargeList(int rechargeId) {
        ConcurrentHashMap<Integer, ActRecharge> result = actRechargeGroupMap.get(rechargeId);
        if (result == null) {
            logger.error("getActRechargeList error key = " + rechargeId);
        }
        return result;
    }
    public static ActRecharge getActRechargeByType(int rechargeId, int type) {
        ConcurrentHashMap<Integer, ActRecharge> rechargeList = getActRechargeList(rechargeId);
        if (rechargeList == null || rechargeList.isEmpty()) {
            logger.error("getActRechargeByType error key = " + rechargeId);
            return null;
        }
        return rechargeList.get(type);
    }

    private static ConcurrentHashMap<Integer, ActRechargeTask> actRechargeTaskMap = new ConcurrentHashMap<>(); // 限时充值活动配置表
    private static ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActRechargeTask>> actRechargeTaskGroupMap = new ConcurrentHashMap<>(); // 限时充值活动配置表

    /**
     * 初始化限时充值活动配置表
     */
    public static Future<Void> initActRechargeTask() {
        String table = "act_recharge_task";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + " order by num asc;";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ActRechargeTask> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActRechargeTask>> tmpGroup = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ActRechargeTask entity = new ActRechargeTask(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        tmpGroup.computeIfAbsent(entity.getTaskGroupId(), k -> new ConcurrentHashMap<>()).put(entity.getId(), entity);
                    }
                    actRechargeTaskMap = tmp;
                    actRechargeTaskGroupMap = tmpGroup;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ActRechargeTask getActRechargeTask(int id) {
        ActRechargeTask result = actRechargeTaskMap.get(id);
        if (result == null) {
            logger.error("getActRechargeTask error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ActRechargeTask> getActRechargeTaskMap() { return actRechargeTaskMap; }

    public static ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ActRechargeTask>> getActRechargeTaskGroupMap() { return actRechargeTaskGroupMap; }

    public static ConcurrentHashMap<Integer, ActRechargeTask> getActRechargeTaskList(int taskGroupId) {
        return actRechargeTaskGroupMap.get(taskGroupId);
    }

    public static List<ActRechargeTask> getActRechargeTaskListByType(int taskGroupId, int type) {
        ConcurrentHashMap<Integer, ActRechargeTask> tmp = getActRechargeTaskList(taskGroupId);
        if (tmp == null || tmp.isEmpty()) {
            return null;
        }
        List<ActRechargeTask> result = new ArrayList<>();
        for (ActRechargeTask entity : tmp.values()) {
            if (entity.getTaskType() == type) {
                result.add(entity);
            }
        }
        return result;
    }
    private static ConcurrentHashMap<Integer, ActRechargeTaskType> actRechargeTaskTypeMap = new ConcurrentHashMap<>(); // 限时充值任务类型配置表

    /**
     * 初始化限时充值任务类型配置表
     */
    public static Future<Void> initActRechargeTaskType() {
        String table = "act_recharge_task_type";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ActRechargeTaskType> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ActRechargeTaskType entity = new ActRechargeTaskType(result.getJsonObject(i));
                        tmp.put(entity.getTaskType(), entity);
                    }
                    actRechargeTaskTypeMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ActRechargeTaskType getActRechargeTaskType(int id) {
        ActRechargeTaskType result = actRechargeTaskTypeMap.get(id);
        if (result == null) {
            logger.error("getActRechargeTaskType error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ActRechargeTaskType> getActRechargeTaskTypeMap() { return actRechargeTaskTypeMap; }

}
